<?php

namespace app\common\model\user;

use app\common\model\addonAuth\AddonGroup;
use app\common\model\BaseModel;
use app\common\model\system\Menu;
use app\common\services\Auth;

class User extends BaseModel
{
    /**
     * 主键
     * @var string
     */
    protected $pk = 'uid';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'User';

    /**
     * 隐藏属性
     * @var array
     */
    protected $hidden = [
        'password',
        'is_delete',
        'salt',
    ];

    /**
     * 只读属性
     * @var array
     */
    protected $readonly = [
        'uid',
        'username',
    ];

    /**
     * 字段类型或者格式转换
     * @var array
     */
    protected $type = [
        'uid'       => 'integer',
        'group_id'  => 'integer',
        'status'    => 'integer',
        'is_delete' => 'integer',
    ];

    /**
     * 定义全局的查询范围
     * @var string[]
     */
    protected $globalScope = [
        'IsDelete',
    ];

    public function group()
    {
        return $this->hasOne(UserGroup::class, 'id', 'group_id')->field('group_name,level,status,rules');
    }

    /**
     * 获取钱包内容
     * @param $value
     * @param $data
     * @return UserWallet|array|\think\Model
     */
    public function getWalletAttr($value, $data)
    {
        $wallet = UserWallet::where(['uid' => $data['uid']])->append(['use_count'])->findOrEmpty()->hidden(['create_time', 'update_time', 'uid']);
        if ($wallet->isEmpty()) {
            $wallet->uid = $data['uid'];
            $wallet->wechat = 0;
            $wallet->free = 0;
            $wallet->work_wechat = 0;
            $wallet->wxapp = 0;
            $wallet->douyin = 0;
            $wallet->min_app = 0;
            $wallet->save();
            $wallet->use_count = [];
        }
        return $wallet;
    }

    /**
     * 获取用户角色权限列表
     * @param $value
     * @param $data
     * @return array
     */
    public function getRolesAttr($value, $data)
    {
        //这里判断是从【客户端、控制台、平台】、应用来的
        $roles = [];
        if (isset($this->group->rules) && $this->is_admin) {
            $roles = (new Menu())->getAuthRoles($this->group->rules);
        }
        $roles[] = 'customer';//登录后就给普通身份
        $is_founder = (new Auth())->isFounder($data['uid']);
        if ($is_founder) {
            $roles[] = 'founder';
        }
        // 判断当前用户是否为超级管理员
        if ((isset($data['is_admin']) && $data['is_admin'] == 1) || $is_founder) {
            $roles[] = 'administrator';
        }
        return $roles;
    }

    /**
     * 获取应用角色权限列表
     * @param $value
     * @param $data
     * @return array|mixed
     */
    public function getAddonRolesAttr($value, $data)
    {
        $UserServices = new \app\common\services\User();
        $addonAuths = $UserServices->getUserAllAddons($data['uid']);
        $addon_name = get_addon();
        if (isset($addonAuths[$addon_name])) {
            if (count($addonAuths[$addon_name]) == 0) {
                return ['all'];
            } else {
                return $addonAuths[$addon_name];
            }
        } else {
            return ['no'];
        }
    }

    /**
     * 过滤被删除的用户
     * @param $query
     */
    public function scopeIsDelete($query)
    {
        $query->where('is_delete', '=', 0);
    }


    public function getGroupNameAttr($value, $data)
    {
        if ((new Auth())->isFounder($data['uid'])) {
            return '创始人';
        } else {
            return UserGroup::where(['id' => $data['group_id']])->value('group_name');
        }
    }

    public function getAddonGroupNameAttr($value, $data)
    {
        return AddonGroup::where(['id' => $data['addon_group_id']])->value('name');
    }


}
